package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.VipLevelDTO;
import com.movie.service.VipLevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vip-levels")
@RequiredArgsConstructor
@Tag(name = "VIP等级管理", description = "VIP等级配置和管理接口")
public class VipLevelController {
    
    private final VipLevelService vipLevelService;
    
    @GetMapping
    @Operation(summary = "获取所有VIP等级列表")
    public ApiResponse<List<VipLevelDTO>> getAllVipLevels() {
        List<VipLevelDTO> vipLevels = vipLevelService.findAll();
        return ApiResponse.success(vipLevels);
    }
    
    @GetMapping("/active")
    @Operation(summary = "获取启用的VIP等级列表")
    public ApiResponse<List<VipLevelDTO>> getActiveVipLevels() {
        List<VipLevelDTO> vipLevels = vipLevelService.findActiveLevels();
        return ApiResponse.success(vipLevels);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取VIP等级")
    public ApiResponse<VipLevelDTO> getVipLevelById(
            @Parameter(description = "VIP等级ID") @PathVariable Long id) {
        VipLevelDTO vipLevel = vipLevelService.findById(id);
        return ApiResponse.success(vipLevel);
    }
    
    @GetMapping("/name/{name}")
    @Operation(summary = "根据名称获取VIP等级")
    public ApiResponse<VipLevelDTO> getVipLevelByName(
            @Parameter(description = "VIP等级名称") @PathVariable String name) {
        VipLevelDTO vipLevel = vipLevelService.findByName(name);
        return ApiResponse.success(vipLevel);
    }
    
    @GetMapping("/level/{level}")
    @Operation(summary = "根据等级值获取VIP等级")
    public ApiResponse<VipLevelDTO> getVipLevelByLevel(
            @Parameter(description = "VIP等级值") @PathVariable Integer level) {
        VipLevelDTO vipLevel = vipLevelService.findByLevel(level);
        return ApiResponse.success(vipLevel);
    }
    
    @PostMapping
    @Operation(summary = "创建VIP等级")
    public ApiResponse<VipLevelDTO> createVipLevel(
            @Parameter(description = "VIP等级信息") @RequestBody VipLevelDTO vipLevelDTO) {
        VipLevelDTO created = vipLevelService.create(vipLevelDTO);
        return ApiResponse.success("VIP等级创建成功", created);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新VIP等级")
    public ApiResponse<VipLevelDTO> updateVipLevel(
            @Parameter(description = "VIP等级ID") @PathVariable Long id,
            @Parameter(description = "VIP等级信息") @RequestBody VipLevelDTO vipLevelDTO) {
        VipLevelDTO updated = vipLevelService.update(id, vipLevelDTO);
        return ApiResponse.success("VIP等级更新成功", updated);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除VIP等级")
    public ApiResponse<Void> deleteVipLevel(
            @Parameter(description = "VIP等级ID") @PathVariable Long id) {
        vipLevelService.delete(id);
        return ApiResponse.success("VIP等级删除成功", null);
    }
    
    @PatchMapping("/{id}/toggle-status")
    @Operation(summary = "切换VIP等级状态")
    public ApiResponse<VipLevelDTO> toggleVipLevelStatus(
            @Parameter(description = "VIP等级ID") @PathVariable Long id) {
        VipLevelDTO updated = vipLevelService.toggleStatus(id);
        return ApiResponse.success("VIP等级状态切换成功", updated);
    }
}